<?php

namespace App\Services\Strator;

use App\Models\Strator\StratorProductAttr;

class StratorProductAttrService
{

    //
    public function getStratorProductAttrs(): array{
        $stratorProductAttrs=StratorProductAttr::with(['parent'])
            ->where('pid',0)
            ->orderBy('sort','asc')
            ->get()
            ->toArray();
        foreach ($stratorProductAttrs as $key=>$ProductAttr){
            $stratorProductAttrs[$key]['children']=$this->getStratorProductAttrsByPid($ProductAttr['id']);
        }
        return $stratorProductAttrs;
    }

    //
    public function getStratorProductAttrsByPid($pid): array{
        $children=StratorProductAttr::with(['parent'])->where('pid',$pid)
            ->orderBy('sort','asc')
            ->get()->toArray();
        foreach ($children as $key=>$ProductAttr){
            $children[$key]['children']=$this->getStratorProductAttrsByPid($ProductAttr['id']);
        }
        return $children;
    }

}
